package utils;

import android.content.Context;
import android.util.Log;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;

import java.util.List;

public class DBUtils {
    private static DbUtils dbUtils;

    public static void createDB(Context context) {
        DbUtils.DaoConfig daoConfig = new DbUtils.DaoConfig(context);
        daoConfig.setDbName("shoppingCart");
        daoConfig.setDbVersion(1);
        daoConfig.setDbDir(context.getCacheDir().getPath());
        daoConfig.setDbUpgradeListener(new DbUtils.DbUpgradeListener() {
            @Override
            public void onUpgrade(DbUtils db, int oldVersion, int newVersion) {

            }
        });

        dbUtils = DbUtils.create(daoConfig);
        Log.i("query", "" + "创建数据库");
        createTable();
    }

    private static void createTable() {
        try {
            dbUtils.createTableIfNotExist(ShoppingCart.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        Log.i("query", "" + "创建表");
    }

    /**
     * 添加数据到购物车
     *
     * @param productID   商品ID
     * @param productName 商品名称
     * @param store       店铺名称
     * @param price       商品价格
     * @param pic_url     图片地址
     * @param stock       库存
     * @return 是否添加成功
     */
    public static boolean addShoppingCart(int productID, String productName, String store, String price, String pic_url, int stock) {
        int count = 0;
        boolean isSuccess = false;
        ShoppingCart cart_ = getShoppingCart(productID);
        //数据存在设置数据
        if (cart_ != null) {
            count = cart_.getCount();
            if (stock > ++count) {
                cart_.setCount(count);
                isSuccess = true;
            }
            try {
                dbUtils.update(cart_, new String[]{"count"});
            } catch (DbException e) {
                e.printStackTrace();
                Log.e("====================", "addShoppingCart: +++++++++++++++++++++++++");
            }
        } else {
            ShoppingCart cart = new ShoppingCart();
            cart.setProductID(productID);
            cart.setProductName(productName);
            cart.setPrice(price);
            cart.setStore(store);
            if (stock > ++count) {
                cart.setCount(count);
                isSuccess = true;
            }
            cart.setPic_url(pic_url);
            cart.setStock(stock);
            try {
                dbUtils.save(cart);
            } catch (DbException e) {
                e.printStackTrace();
                Log.e("====================", "addShoppingCart: +++++++++++++++++++++++++");
            }
        }
        return isSuccess;
    }

    public static ShoppingCart getShoppingCart(int productID) {
        ShoppingCart cart = null;
        try {
            cart = dbUtils.findById(ShoppingCart.class, productID);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return cart;
    }

    /**
     * 获取商品列表
     */
    public static List<ShoppingCart> getAllShoppingCart() {
        List<ShoppingCart> list = null;
        try {
            list = dbUtils.findAll(ShoppingCart.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据id减少数量
     *
     * @param productID 商品ID
     */
    public static void minusShoppingCart(int productID) {
        ShoppingCart cart_ = getShoppingCart(productID);
        if (cart_ != null) {
            int count = cart_.getCount();
            cart_.setCount(count--);
            if (count <= 0) {
                try {
                    dbUtils.delete(cart_);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    dbUtils.update(cart_, new String[]{"count"});
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
